package com.dbapp.shopping;

import com.dbapp.shopping.data.UserRepository;
import com.dbapp.shopping.data.GoodOwnerRepository;
import com.dbapp.shopping.data.RecordRepository;
import com.dbapp.shopping.data.GoodRepository;
import com.dbapp.shopping.domain.GoodOwner;
import com.dbapp.shopping.domain.User;
import com.dbapp.shopping.domain.Good;
import com.dbapp.shopping.domain.GoodOwner;
import com.dbapp.shopping.domain.Record;

import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.Paths;

import com.dbapp.shopping.storage.StorageService;
import javafx.scene.shape.Path;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.util.StringUtils;

@SpringBootApplication
public class ShoppingApplication {

	public static void main(String[] args) {
		SpringApplication.run(ShoppingApplication.class, args);
	}
	@Bean
	public CommandLineRunner userDataLoader(UserRepository repo, GoodRepository goodRepo,
											GoodOwnerRepository goodOwnerRepo, RecordRepository recordRepo,
											StorageService storageService) {
		return new CommandLineRunner() {
			@Override
			public void run(String... args) throws Exception {
				storageService.deleteAll();
				storageService.init();
				PasswordEncoder passwordEncoder = new StandardPasswordEncoder("53cr3t");
				User empty = preloadUser("empty", "0000", "emptyuser", passwordEncoder);
				User u1 = preloadUser("gkzhb", "1998", "zhb", passwordEncoder);
				User u2 = preloadUser("x54", "990729", "xsh", passwordEncoder);
				Good g = preloadGoods("算法导论 机械工业出版社 原书第三版",
						"该书是一本十分经典的计算机算法书籍，与高德纳（Donald E.Knuth）的《计算机程序设计艺术》（The Art Of Computer Programming）相媲美。 " +
								"《算法导论》由Thomas H.Cormen、Charles E.Leiserson、Ronald L.Rivest、Clifford Stein四人合作编著（其中Clifford Stein是第二版开始参与的合著者）。" +
								"本书的最大特点就是将严谨性和全面性融入在了一起。",
						100L, 108.0, "src/main/resources/static/assets/img/sample.jpg");
				GoodOwner go = preloadGoodOwner(g, u1);
			}

			public Good preloadGoods(String name, String description, Long stock, Double price, String path)
			{
				Good g = new Good(name, description, stock, price);
				Good saved = goodRepo.save(g);
				File img = new File(path);
				String newName = saved.getId().toString() + "." + StringUtils.getFilenameExtension(path);
				try{
					Files.copy(Paths.get(path), Paths.get("D:/PJImg/goodsImg/" + newName));
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				saved.setAvataruri("PJImg/goodsImg/" + newName);
				goodRepo.save(saved);
				return saved;
			}

			public User preloadUser(String  username, String password, String name, PasswordEncoder passwordEncoder)
			{
				User user = new User(username);
				user.setPassword(passwordEncoder.encode(password));
				user.setName(name);
				User saved = repo.save(user);
				return saved;
			}

			public GoodOwner preloadGoodOwner(Good good, User user)
			{
				GoodOwner go = new GoodOwner();
				go.setGood(good);
				go.setUser(user);
				GoodOwner saved = goodOwnerRepo.save(go);
				return saved;
			}
		};
	}
}
